package com.woniuxy.lab.woniu_lab_boss.util;

import com.woniuxy.lab.woniu_lab_boss.exception.WebException;
import com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode;

public class PathUtil {

    private static final String linuxFileSavePath = "/mnt/nfs_share/upload/temp/files/";
    private static final String windowsFileSavePath = "C:/files/";
    private static final String httpPrfix = "http://43.136.111.122:8090";
    public static final String linuxSavePath = "/mnt/nfs_share/upload/files/";// 文件类型靶场最终存储路径

    public static String getFileSavePath(){
        String OSName = System.getProperties().getProperty("os.name");
        if (OSName.startsWith("Windows"))
            return windowsFileSavePath;
        else
            return linuxFileSavePath;
    }

    /**
     *
     * @param localPath /root/files/1679474873802@接口样例.docx or C:/files/1679474873802@接口样例.docx
     * @return http://192.168.120.10:8090/files/1679474873802@接口样例.docx
     */
//    public static String local2http(String localPath){
//        if(localPath.startsWith(linuxFileSavePath)){
//            return httpPrfix + localPath.replace("/root", "");
//        }
//        else if(localPath.startsWith(windowsFileSavePath)){
//            return httpPrfix + localPath.replace("C:", "");
//        }else{
//            System.out.println(localPath);
//            throw new WebException(WebExceptionCode.PATH_FORMAT_ERROR);
//        }
//    }

    /**
     *
     * @param httpPath http://192.168.120.10:8090/files/1679474873802@接口样例.docx
     * @return /root/files/1679474873802@接口样例.docx
     */
    public static String http2local(String httpPath){
        String res = "";
        if(httpPath.startsWith("http://43")){
            res = "/mnt/nfs_share/upload/temp"+httpPath.replace(httpPrfix, "");
        } else if (httpPath.startsWith("http://localhost")) {
            res = "/mnt/nfs_share/upload/temp"+httpPath.replace("http://localhost:8090", "");
        }
        return res;
    }
    // http://43.136.111.122:8090/files/xxx.zip
    // /mnt/nfs_share/upload/temp/files/xxx.zip
    public static String http2raw(String httpPath){
        String oldStr = httpPrfix + "/files/";
        String newStr = linuxFileSavePath;
        return httpPath.replace(oldStr, newStr);
    }

//    /**
//     * @param linux  /root/files/1679474873802@接口样例.docx
//     * @return C:/files/1679474873802@接口样例.docx
//     */
//    public static String linux2windows(String linux){
//        return "C:"+linux.replace("/root", "");
//    }
//
//    /**
//     * @param windows C:/files/1679474873802@接口样例.docx
//     * @return /root/files/1679474873802@接口样例.docx
//     */
//    public static String windows2linux(String windows){
//        return "/root"+windows.replace("C:", "");
//    }
    
}
